col4 bit P2.7
col3 bit P2.6
col2 bit P2.5
col1 bit P2.4
row1 bit P2.3
row2 bit P2.2
row3 bit P2.1
row4 bit P2.0
keydata equ 70h
keybounc equ 71h
keyport equ P2
org 0h
	mov P2,#11111111b
	call Init_LCD
	mov R1, #80h
	Mov R6, #01h
	CLR P3.6
	call write_inst
start: call keypad4x4 ;calling subroutine keypad4x4
	Mov A,keydata ;A = keydata
	Cjne A,#0FFh,WrLCD;
	sjmp start;LOOPING FOREVER PART 1
;
WrLCD: 	;Mov R1,#80h ;Pick DDRAM 1st row and 1st col
	CALL SaveRatusan
	INC R6
	Mov R1,#30h
	Add A,R1
	Mov R1,A
	call write_data ;write data
	;djnz R5,start
	Sjmp start ;LOOPING FOREVER PART 2;
;
WrLCD2: 	
	Mov R1,#30h
	Add A,R1
	Mov R1,A
	call write_data ;write data
	RET
WrLCD3: 	
	Mov R1,#30h
	Add A,R1
	Mov R1,A
	call write_data ;write data
	SJMP START
SaveRatusan:
	CJNE R6,#1D, SavePuluhan
	MOV R3, A
RET
SavePuluhan:
	CJNE R6,#2D, SaveSatuan
	MOV R4, A
RET
SaveSatuan:
	CJNE R6,#3D, WRLCD3
	MOV R5, A
RET

stop: sjmp stop
Init_lcd:
	mov r1,#00000001b ;Display clear
	call write_inst
	mov r1,#00111000b ;Function set,Data 8 bit,2 line font 5x7
	call write_inst
	mov r1,#00001100b ;Display on,cursor off,cursor blink off
	call write_inst
	mov r1,#00000110b ;Entry mode,Set increment
	call write_inst
ret
;
Write_inst:
	clr P3.5 ;P3.5 = RS =0
	mov P0,R1 ;copy R1 ke P0
	setb P3.7 ;P3.7 =EN = 1
	call delay ;call delay time
	clr P3.7 ;P3.7 =EN = 0
ret
;
Write_data:
	setb P3.5 ;P3.5 = RS =1
	mov P0,R1 ;copy R1 ke P0
	;mov P0,R3
	;mov P0,R4
	setb P3.7 ;P3.7 =EN = 1
	call delay ;call delay time
	clr p3.7 ;P3.7 =EN = 0
ret
ValidasiRatusan:
RET
ValidasiPuluhan:
RET
ValidasiSatuan:
RET
;====================================
; subroutine scan keypad 4x4
;====================================
Keypad4x4:
	mov keybounc,#50 ;keybounc = 50
	mov keyport,#0FFh ;keyport=P2= FF
	clr col4 ;col4 = 0
;
keyCOR: jb row4,keyMEN ; Key COR
	djnz keybounc,KeyCOR
	mov keydata,#0Ah ;Data Output
ret
;
keyMEN: jb row3,keyUpA ; Key MEN
	djnz keybounc,keyMEN
	mov keydata,#0bh ;Data Output
ret
keyUpA: jb row2,keyDnA ; Key Up Arrow
	djnz keybounc,keyUpA
	mov keydata,#0ch ;Data Output
ret
;
keyDnA: jb row1,key3 ; Key Down Arrow
	djnz keybounc,keyDnA
	mov keydata,#0dh ;Data Output
ret
;==========================================
key3: 	setb col4
	clr col3
	jb row4,key6
	djnz keybounc,key3 ; Key 3
	mov keydata,#03h ;Data Output
ret
;
key6: jb row3,key9
	djnz keybounc,key6 ; Key 6
	mov keydata,#06h ;Data Output
ret
;
key9: jb row2,keyENT
	djnz keybounc,key9 ; Key 9
	mov keydata,#09h ;Data Output
ret
;
keyENT: jb row1,key2
	djnz keybounc,keyENT ; Key ENT
	call init_lcd
	MOV A, R4
	MOV B, #10
	MUL AB
	ADD A, R5
	MOV R7, A
	MOV A, R3
	MOV B, #100
	MUL AB
	ADD A, R7
	MOV B, #36

	DIV AB
	MOV 10h, A
	MOV 11h, B
	MOV A, B
	PUSH A
	MOV A, 10H
	PUSH A
	MOV R1,#80h

	POP A
	MOV DPTR,#DataBase36
	MOVC A,@A+DPTR
	CALL WrLCD2

	POP A
	MOV DPTR,#DataBase36
	MOVC A,@A+DPTR
	CALL WrLCD2
	
	clr A
	MOV R3, A
	MOV R5, A
	MOV R4, A
	MOV R6, #01h
	LJMP start
;============================================
key2: 	setb col3
	clr col2
	jb row4,key5
	djnz keybounc,key2
	mov keydata,#02h ;Data Output
ret
;
key5: 	jb row3,key8
	djnz keybounc,key5
	mov keydata,#05h ; Data Output
ret
;
key8: 	jb row2,key0
	djnz keybounc,key8
	mov keydata,#08h ;Data Output
ret
;
key0: 	jb row1,key1
	djnz keybounc,key0
	mov keydata,#00h ;Data Output
ret
;==============================================
key1: 	setb col2
	clr col1
	jb row4,key4
	djnz keybounc,key1
	mov keydata,#01h ;Data Output
ret
;
key4: 	jb row3,key7
	djnz keybounc,key4
	mov keydata,#04h ;Data Output
ret
;
key7: 	jb row2,keyCAN
	djnz keybounc,key7
	mov keydata,#07h ;Data Output
ret
;
keyCAN: jb row1,Nokey
	djnz keybounc,keyCAN
	;mov keydata,#0FFh
	MOV R6, #01h
	call init_lcd
ret
;
Nokey: mov keydata,#0FFh
ret
;================================
;The end of Keypad 4x4 subroutine
;================================
delay:	mov R0,#01h
delay1: mov R2,#0ffh
	;djnz R2,$
	djnz R0,delay1
ret

DataBase36:
	db '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
end